﻿// -------------------------------------------------------------------------------------------------
// <copyright file="ZipFileHandler.cs" company="HeterogeneousComputing.com">
// Creative Commons Attribution-ShareAlike 3.0 Unported License
// </copyright>
//
// <author>Jock</author>
//
// <date>04/08/2011</date>
//
// <summary>Implements the zip file handler class</summary>
// -------------------------------------------------------------------------------------------------
namespace TheTVDBScraper
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

    /// <summary>
    /// Utility Class of the TheTVDB Scraper for handling ZIP files returned
    /// when we GetBaseInfo for a given TVSeries.
    /// </summary>
    internal class ZipFileHandler
    {
        #region Internal Operations
            /// <summary>
            /// Tries to open the supplied filename as a ZIP file.
            /// </summary>
            /// <param name="filename">
            /// The name of the downloaded ZIP file from TheTVDB
            /// which we wish to open and parse out the
            /// language.xml
            /// banner.xml
            /// actors.xml
            /// files.
            /// </param>
            /// <returns>
            /// Return a Dictionary keyed on the XML file name e.g. en.xml
            /// and with the actual XML data as the value.
            /// </returns>
            internal Dictionary<string, string> OpenZipFile(string filename)
            {
                Dictionary<string, string> xmlFileDictionary = new Dictionary<string, string>();
                if (!File.Exists(filename))
                {
                    Console.WriteLine("Cannot find file '{0}'", filename);
                    return null;
                }

                using (ZipInputStream zis = new ZipInputStream(File.OpenRead(filename)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = zis.GetNextEntry()) != null)
                    {
                        Console.WriteLine(theEntry.Name);

                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        string xmlString = this.ReadXmlFileToString(zis);
                        xmlString = xmlString.Replace('\0', ' ');
                        xmlFileDictionary.Add(fileName, xmlString);
                        Console.WriteLine(xmlString);
                    }
                }

                return xmlFileDictionary;
            }
        #endregion

        #region Private Operations
            /// <summary>
            /// Reads the content (byte[]) of an entry within the ZIP file
            /// into a StringBuilder.
            /// </summary>
            /// <param name="zis">
            /// The ZipInputStream which we are to read from.
            /// </param>
            /// <returns>
            /// A string containing the XML.
            /// </returns>
            private string ReadXmlFileToString(ZipInputStream zis)
            {
                StringBuilder xmlFileString = new StringBuilder();
                Encoding encoding = Encoding.UTF8;
                int size = 2048;
                while (true)
                {
                    byte[] data = new byte[size];
                    size = zis.Read(data, 0, data.Length);
                    if (size > 0)
                    {
                        xmlFileString.Append(encoding.GetString(data));
                    }
                    else
                    {
                        break;
                    }
                }

                return xmlFileString.ToString();
            }
        #endregion
    }
}

/// -------------------------------------------------------------------------------------------------
// End of ZipFileHandler.cs
/// -------------------------------------------------------------------------------------------------
